package com.hzhr.iot.login.sdk.service;

import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrHttpUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.login.sdk.constant.Login_ErrorsEnum;
import com.hzhr.iot.login.sdk.model.userCard.UserCardGetModel;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class Login_UserCardService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 查询用户名片 <br/><br/>
     * 简介&nbsp;:&nbsp;所有用户名片 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询用户名片&nbsp;:&nbsp;根据路径参数“令牌”查询用户卡片“用户卡片_1”；(√) <br/>
     * 2.&nbsp;返回信息&nbsp;:&nbsp;根据“用户卡片_1”生成并返回“用户名片模型(查询)”；(√) <br/>
     * @param header <br/><br/>
     * @param query <br/><br/>
     * @param token 令牌 <br/><br/>
     * @throws ServiceException <br/>
     * @see UserCardGetModel result <br/>
     */ 
    public UserCardGetModel getUserCard(Map<String, String> header, String token, Map<String, Object> query) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(null, headers);

        Map<String, Object> uriVariables = new HashMap<>();
        uriVariables.put("token", token);

        try {
            ResponseEntity<UserCardGetModel> result = restTemplate.exchange("http://login-service/user-cards/{token}?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.GET, httpEntity, UserCardGetModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Login_ErrorsEnum.getEnumByName("USERCARDSERVICE_GET_USER_CARD_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Login_ErrorsEnum.getEnumByName("USERCARDSERVICE_GET_USER_CARD_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
}

